<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <PageTools>
        <el-button
          slot="after"
          size="small"
          type="primary"
          @click="addPermission('1', '0')"
        >新增权限</el-button>
      </PageTools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column
          header-align="center"
          label="名称"
          prop="name"
        ></el-table-column>
        <el-table-column
          align="center"
          label="标识"
          prop="code"
        ></el-table-column>
        <el-table-column
          align="center"
          label="描述"
          prop="description"
        ></el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="addPermission('2', row.id)"
            >添加</el-button>
            <el-button
              type="text"
              @click="editPermission(row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              @click="delPermission(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :visible="showDialog"
      :title="formData.id ? '编辑权限' : '新增权限'"
      @close="close"
    >
      <el-form
        ref="perFormRef"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="120px"
        label-position="left"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入权限名称"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            v-model="formData.code"
            placeholder="请输入权限标识"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input
            v-model="formData.description"
            placeholder="请输入权限描述"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="close">取消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="handelConfirm"
        >确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  components: {},
  props: {},
  data() {
    return {
      list: [],
      formData: {
        name: undefined,
        code: undefined,
        description: undefined,
        enVisible: '0',
        type: undefined,
        pid: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '权限名称不能为空',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '权限标识不能为空',
          trigger: 'blur'
        }]
      },
      showDialog: false // 控制编辑新增的弹层
    }
  },
  created() {
    this.getPermissionList()
  },
  mounted() {},
  methods: {
    // 编辑权限
    async editPermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 删除权限
    async delPermission(id) {
      try {
        await this.$confirm('确认删除?')
        await delPermission(id)
        this.$message.success('删除权限成功')
        this.getPermissionList()
      } catch (e) {
        console.log(e)
      }
    },
    // 新增权限
    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    // 获取权限列表
    async getPermissionList() {
      this.list = tranListToTreeData(await getPermissionList(), '0')
    },
    // 关闭弹层
    close() {
      // 关闭弹窗清空表单数据
      this.formData = this.$options.data().formData
      this.$refs['perFormRef'].resetFields()
      this.showDialog = false
    },
    // 点击确认
    handelConfirm() {
      this.$refs['perFormRef'].validate(async valid => {
        if (!valid) return
        this.formData.id ? await updatePermission(this.formData) : await addPermission(this.formData)
        this.$message.success('操作成功')
        this.getPermissionList()
        this.close()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
